import { _cartList, _cartCheckedUpdate, _cartGoodsUpdate, _cartDeleteGoods } from "../../service/api";

Page({
  data: {
    cartList: [],
    // 价格、数量
    cartTotal: {
      count: 0,
      totalPrice: 0,
      checkedCount: 0,
      checkedTotalPrice: 0,
    },
    isEditCart: false, // 是否编辑
    checkedAll: true, // 全选状态
  },
  onShow: function (options) {
    this.getCartList();
  },
  async getCartList() {
    const { cartList, cartTotal } = await _cartList();
    this.setData({ cartList, cartTotal });
    this.setData({ checkedAll: this.isAllChecked() });
  },
  // 编辑
  handleEditCart() {
    let { isEditCart, cartList } = this.data;
    if (isEditCart) {
      // 编辑完成, 重新发送请求获取编辑后的数据
      this.getCartList();
      this.setData({ isEditCart: !isEditCart });
    } else {
      // 编辑状态
      cartList.map((item) => (item.checked = false));
      this.setData({ cartList, isEditCart: !isEditCart });
      this.changeComputePrice();
    }
  },
  // 选择与取消
  async handleCheckedItem(e) {
    const index = e.target.dataset.itemIndex;
    let { isEditCart, cartList } = this.data;
    let cartItem = cartList[index];
    if (isEditCart) {
      // 编辑状态
      cartItem.checked = !cartItem.checked;
      this.setData({ cartList });
      this.changeComputePrice();
    } else {
      const productIds = cartItem.product_id;
      const checked = cartItem.checked ? 0 : 1;
      const res = await _cartCheckedUpdate({ productIds, checked });
      if (res.errno === 200) {
        cartItem.checked = checked;
        this.setData({ cartList });
        this.changeComputePrice();
      }
    }
  },
  // 全选
  async handleCheckedAll() {
    let { isEditCart, cartList } = this.data;
    if (isEditCart) {
      // 编辑状态
      const checkedStatus = this.isAllChecked();
      cartList.map((item) => (item.checked = !checkedStatus));
      this.setData({ cartList });
      this.changeComputePrice();
    } else {
      const productIds = cartList.map((item) => item.product_id).join(",");
      const checked = this.isAllChecked() ? 0 : 1;
      const res = await _cartCheckedUpdate({ productIds, checked });
      if (res.errno === 200) {
        cartList.forEach((item) => (item.checked = checked));
        this.setData({ cartList });
        this.changeComputePrice();
      }
    }
  },
  // 删除商品
  async handleDeleteItem() {
    const productIds = this.data.cartList
      .filter((item) => item.checked)
      .map((element) => element.product_id)
      .join(",");
    if (productIds) {
      const res = await _cartDeleteGoods({ productIds });
      if (res.errno === 200) {
        const cartList = res.data.cartList.map((item) => (item.checked = false));
        this.setData({ cartList });
        this.changeComputePrice();
      }
    }
  },
  // 判断是否全选
  isAllChecked() {
    return this.data.cartList.every((item) => item.checked == true);
  },
  // 重新计算总价格、选择状态
  changeComputePrice() {
    let count = 0;
    let totalPrice = 0.0;
    let checkedCount = 0;
    let checkedTotalPrice = 0.0;
    for (let item of this.data.cartList) {
      count += item.number;
      totalPrice += item.number * item.retail_price;
      if (item.checked) {
        checkedCount += item.number;
        checkedTotalPrice += item.number * item.retail_price;
      }
    }
    this.setData({
      cartTotal: { count, totalPrice, checkedCount, checkedTotalPrice },
      checkedAll: this.isAllChecked(),
    });
  },
  // 数量减一
  handleCutNumber(e) {
    const index = e.target.dataset.itemIndex;
    let cartItem = this.data.cartList[index];
    if (cartItem.number > 1) {
      cartItem.number = cartItem.number - 1;
      this.setData({ cartList: this.data.cartList });
      this.updateGoodsCount(cartItem.product_id, cartItem.goods_id, cartItem.number, cartItem.id);
    }
  },
  // 数量加一
  handleAddNumber(e) {
    const index = e.target.dataset.itemIndex;
    let cartItem = this.data.cartList[index];
    cartItem.number = cartItem.number + 1;
    this.setData({ cartList: this.data.cartList });
    this.updateGoodsCount(cartItem.product_id, cartItem.goods_id, cartItem.number, cartItem.id);
  },
  // 更新商品数量
  async updateGoodsCount(productId, goodsId, number, id) {
    await _cartGoodsUpdate({ productId, goodsId, number, id });
    this.changeComputePrice();
  },
  // 下单
  handleCheckoutOrder() {
    const checkeds = this.data.cartList.filter((item) => item.checked == true);
    if (checkeds.length > 0) {
      wx.navigateTo({
        url: "/pages/shopping/confirm_order/comfirm_order",
      });
    } else {
      wx.showToast({
        title: "最少要选择一件商品",
        icon: "error",
        mask: true,
      });
    }
  },
});
